home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / scsh-0.4 / scsh-0 / scsh-0.4.2 / dynload.c < prev    next >
C/C++ Source or Header  |  1995-11-04  |  13KB  |  478 lines

  1.  
  2. /* Dynamic loading.  Copyright unclear. */
  3.  
  4. #include <stdio.h>
  5. #include "sysdep.h"
  6. #include "scheme48.h"
  7.  
  8. /* 
  9.    Hey folks, please help us out with conditions under which the
  10.    ANCIENT_DYNLOAD and/or HAVE_DLOPEN code might work.  About all we
  11.    know is that ANCIENT_DYNLOAD worked once upon a time on the DEC
  12.    MIPS (how to conditionalize for that I don't know -- #ifdef ultrix
  13.    perhaps?), is very similar to something that worked under BSD 4.2,
  14.    and doesn't work under HPUX, NeXT, or SGI.
  15.  */
  16. #if defined(ultrix)
  17. #define ANCIENT_DYNLOAD
  18. #endif
  19.  
  20.  
  21. #if defined(HAVE_DLOPEN)
  22.  
  23. /*-------------------------------------------------------------------
  24.    Following is the preferred modern method, supposedly.  dlopen() is
  25.    some kind of newfangled SYSV thing.  This code was contributed by
  26.    Basile Starynkevitch <basile@soleil.serma.cea.fr> in October 1993 --
  27.    thanks!
  28.  */
  29.  
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #if defined(__NetBSD__) || defined(__FreeBSD__)
  33. #include <nlist.h>
  34. #include <link.h>
  35. #else
  36. #include <dlfcn.h>
  37. #endif
  38.  
  39. #ifdef HAVE_LIBGEN_H
  40. #include <libgen.h>
  41. /* if we have pathfind, get the file name with $LD_LIBRARY_PATH or $S48_EXTERN_PATH */
  42. static char *shared_object_name(char *name)
  43. {
  44.   char *res=0;
  45.   res = pathfind(getenv("LD_LIBRARY_PATH"), name, "r");
  46.   if (res) 
  47.     return res;
  48.   res = pathfind(getenv("S48_EXTERN_PATH"), name, "r");
  49.   if (res) 
  50.     return res;
  51.   return name;
  52. } /* end of shared_object_name */
  53. #define SHARED_OBJECT_NAME(Name) shared_object_name(Name)
  54. #else /* no HAVE_LIBGEN_H */
  55. #define SHARED_OBJECT_NAME(Name) (Name)
  56. #endif /*HAVE_LIBGEN_H*/
  57.  
  58.  
  59. #ifndef MAXNB_DLOPEN
  60. #define MAXNB_DLOPEN 40
  61. #endif /* MAXNB_DLOPEN */
  62.  
  63. #ifndef S48_DLOPEN_MODE
  64. /* SunoS5 & SVR4 define RTLD_NOW */
  65. #ifdef RTLD_NOW
  66. #define S48_DLOPEN_MODE RTLD_NOW
  67. #else 
  68. /* SunOS4 just says that mode should be 1 ie RTLD_LAZY */
  69. #define S48_DLOPEN_MODE 1
  70. #endif /*RTLD_NOW*/
  71. #endif /*!S48_DLOPEN_MODE*/
  72.  
  73. static void* dlopened_handle[MAXNB_DLOPEN];
  74.  
  75. /* with dlopen the only user argument is the full shared object name */
  76. int
  77. dynamic_load(char*sharedobjname)
  78. {
  79.   char pathname[520];
  80.   int  rank= -1;
  81.   int cnt;
  82.   void *newhandle;
  83.   pathname[0]='\0';
  84.   strncpy(pathname, SHARED_OBJECT_NAME(sharedobjname), sizeof(pathname)-1);
  85.   pathname[sizeof(pathname)-1]='\0';
  86.   /* find an unused slot */
  87.   for (cnt=0; cnt<MAXNB_DLOPEN; cnt++)
  88.     if (!dlopened_handle[cnt]) {
  89.       rank=cnt;
  90.       break;
  91.     }
  92.   if (rank<0) {
  93.     fprintf(stderr,
  94.         " dynamic_load - table of dlopened handles is full (MAXNB_DLOPEN=%d) ",
  95.         MAXNB_DLOPEN);
  96.     return -1;
  97.   };
  98.   newhandle=dlopen(pathname, S48_DLOPEN_MODE);
  99. #if defined(__NetBSD__) || defined(__FreeBSD__)
  100.   if (newhandle == -1) {
  101.     fprintf(stderr, " dynamic_load of %s can't dlopen %s",
  102.         sharedobjname, pathname);
  103.     return -1;
  104.   };
  105. #else
  106.   if (!newhandle) {
  107.     fprintf(stderr, " dynamic_load of %s can't dlopen %s: %s ",
  108.         sharedobjname, pathname, dlerror());
  109.     return -1;
  110.   };
  111. #endif
  112.   dlopened_handle[rank] = newhandle;
  113. #ifdef DLDEBUG
  114.   printf(" %s:%d %s sharedobjname='%s' pathname='%s' handle %#x rank=%d \n",
  115.      __FILE__, __LINE__, __FUNCTION__, 
  116.      sharedobjname, pathname, newhandle, rank);
  117. #endif /*DLDEBUG*/
  118.   return 0;
  119. } /*------end of dlopening dynamic load -----*/
  120.  
  121. /* this function implements lookup_external_name with dlsym */
  122. long
  123. lookup_dlsym(char *name, long *location)
  124. {
  125.   void *adr;
  126.   static void *selfhandle;
  127.   int rank;
  128. /* perhaps i should scan the dlopened_handle from last to first,
  129.    to find newest incarnation of symbol? in practice it should be faster
  130.    to go from first (oldest) to last,
  131.    and i hope that every external symbol is defined exactly once most of
  132.    the time!*/
  133.   for (rank=0; rank<MAXNB_DLOPEN; rank++)
  134.     if (dlopened_handle[rank] && (adr=dlsym(dlopened_handle[rank], name))) {
  135.       *location = (long) adr;
  136. #ifdef DLDEBUG
  137.   printf(" %s:%d %s name='%s' in rank=%d at adr=%#x\n",
  138.      __FILE__, __LINE__, __FUNCTION__, 
  139.      name, rank, (long) adr);
  140. #endif /*DLDEBUG*/
  141.       return 1;
  142.     };
  143.   /* find the name in the self process image - ie original scheme48
  144.      executable file */
  145.   if (!selfhandle) 
  146.     selfhandle=dlopen((char*)0, S48_DLOPEN_MODE);
  147.   if (adr=dlsym(selfhandle, name)) {
  148.     *location = (long) adr;
  149. #ifdef DLDEBUG
  150.   printf(" %s:%d %s name='%s' in self at adr=%#x\n",
  151.      __FILE__, __LINE__, __FUNCTION__, 
  152.      name, (long) adr);
  153. #endif /*DLDEBUG*/
  154.     return 1;
  155.   };
  156.   /* can't find name so return 0 */
  157. #ifdef DLDEBUG
  158.   printf(" %s:%d %s name='%s' not found\n",
  159.      __FILE__, __LINE__, __FUNCTION__, 
  160.      name);
  161. #endif /*DLDEBUG*/
  162.   return 0;
  163. } /*----end of lookup_dlsym ---*/
  164.  
  165. #elif defined(ANCIENT_DYNLOAD)
  166.  
  167. /*-------------------------------------------------------------------
  168.    This is the ancient Berkeley method for dynamic loading, using the
  169.    disgusting -A flag to "ld".
  170.  */
  171.  
  172. #include <a.out.h>
  173. #include <sys/types.h>      /* sbrk   */
  174. #include <strings.h>        /* strlen */
  175.  
  176. #ifdef mips /* or should this be ultrix? */
  177. struct exec {               /* an exec-like structure for the MIPS */
  178.   struct filehdr ex_f;
  179.   struct aouthdr ex_o;
  180. };
  181.  
  182. #define a_magic ex_o.magic
  183. #define a_text ex_o.tsize
  184. #define a_data ex_o.dsize
  185. #define a_bss  ex_o.bsize
  186.  
  187. #define BAD_MAGIC(output_file_header) N_BADMAG(output_file_header.ex_o)
  188. #define TEXT_OFFSET(output_file_header) \
  189.         (long) N_TXTOFF(output_file_header.ex_f, output_file_header.ex_o)
  190.  
  191. #else /* not mips */
  192.  
  193. #define BAD_MAGIC(output_file_header) N_BADMAG(output_file_header)
  194. #define TEXT_OFFSET(output_file_header) \
  195.         (long) N_TXTOFF(output_file_header)
  196.  
  197. #endif        
  198.  
  199.  
  200. extern char *object_file;   /* specified via a command line argument */
  201. extern char *reloc_file;
  202.  
  203. extern char *get_reloc_file();
  204.  
  205. /* declarations so that I can put the functions in a reasonable order */
  206. int really_dynamic_load( char *, char *, char * );
  207. void abort_load( char *, FILE *, char *);
  208.  
  209. int
  210. dynamic_load( char *user_args )
  211. {
  212.   char *old_reloc_file, *new_reloc_file;
  213.  
  214.   old_reloc_file = get_reloc_file();
  215.  
  216.   if (old_reloc_file == NULL) {
  217.     fprintf(stderr, "aborting dynamic load\n");
  218.     return(0);
  219.   }
  220.   
  221.   new_reloc_file = (char *) malloc(L_tmpnam);
  222.   
  223.   tmpnam(new_reloc_file);
  224.  
  225.   if (-1 == really_dynamic_load(old_reloc_file, new_reloc_file,
  226.                 user_args)) {
  227.     free(new_reloc_file);
  228.     return(-1);
  229.   }
  230.  
  231.   reloc_file = new_reloc_file;
  232.  
  233.   if (old_reloc_file != object_file)
  234.     if (0 != unlink(old_reloc_file))
  235.       fprintf(stderr, "unable to delete file %s\n", old_reloc_file);
  236.     free(old_reloc_file);
  237.  
  238.   return(0);
  239. }
  240.  
  241. /*
  242.  
  243.   really_dynamic_load() executes an `ld' command that links
  244.   `user_args' into `output_file', including relocation information
  245.   from `reloc_info_file'.  `output_file' is then loaded into the
  246.   current process.
  247.  
  248.   `output_file' can then be used as `reloc_info_file' for subsequent
  249.   dynamic loads.
  250.  
  251.   0 is returned if all goes well, -1 if something goes wrong.
  252.  
  253.   This is just N consecutive system calls with error checking.
  254.   The basic sequence is:
  255.     1) allocate storage for the command string
  256.     2) use sbrk() to align the storage pointer on a page boundary
  257.     3) fill in the command string
  258.     4) execute the command
  259.     5) open the resulting file and read its header
  260.     6) allocate storage for reading in the file
  261.     7) read in the text and data segments
  262.   It is important that the storage pointer does not change between
  263.   steps 2 and 6.  The problem is that ld needs to know the eventual
  264.   starting address of the text, but the amount of text cannot be
  265.   determined until after the ld command.
  266.  
  267. */
  268.  
  269. int
  270. really_dynamic_load( char *reloc_info_file, char *output_file, char *user_args )
  271. {
  272.   int command_length;
  273.   char *command;                /* the ld command */
  274.   int status;
  275.  
  276.   long load_point, actual_load_point, end_of_memory;
  277.  
  278.   FILE *output_file_desc;
  279.   char output_file_buffer[BUFSIZ];
  280.   struct exec output_file_header;
  281.   long loaded_size, required_size;
  282.  
  283.   extern int getpagesize();
  284.   extern char *sbrk(int);
  285.  
  286.   int page_size = getpagesize();
  287.  
  288.   char *template = "/bin/ld -N -x -A %s -T %lx -o %s %s -lc";
  289.  
  290.   /* calculate how long the ld command will be */
  291.   command_length = strlen(template) + strlen(reloc_info_file) +
  292.     strlen(user_args) + 30;  /* additional space for load_point etc. */
  293.  
  294.   command = (char *) malloc(command_length);
  295.  
  296.   if (command == NULL) {
  297.     fprintf(stderr, "malloc failed to allocate %d characters\n",
  298.         command_length);
  299.     abort_load(NULL, NULL, NULL);
  300.     return(-1);
  301.   }
  302.  
  303.   end_of_memory = (long) sbrk(0);
  304.  
  305.   if (end_of_memory < 0) {
  306.     fprintf(stderr, "sbrk(0) failed\n");
  307.     abort_load(command, NULL, NULL);
  308.     return(-1);
  309.   }
  310.   
  311.   /* move the storage pointer to the next page boundary */
  312.   end_of_memory = (long) sbrk(page_size - end_of_memory % page_size);
  313.  
  314.   if (end_of_memory < 0) {
  315.     fprintf(stderr, "sbrk(...) failed\n");
  316.     abort_load(command, NULL, NULL);
  317.     return(-1);
  318.   }
  319.  
  320.   load_point = (long) sbrk(0);    /* no malloc or printf after this */
  321.   
  322.   if (load_point < 0 || 0 != load_point % page_size) {
  323.     fprintf(stderr, "couldn't align sbrk on page boundary\n");
  324.     abort_load(command, NULL, NULL);
  325.     return(-1);
  326.   }
  327.  
  328.   /* finish making the load command */
  329. #ifdef sun  /* Sun's sprintf has a nonstandard return value */
  330.   sprintf(command, template, reloc_info_file, load_point, output_file,
  331.       user_args);
  332. #else
  333.   status = sprintf(command, template, reloc_info_file, load_point, output_file,
  334.            user_args);
  335.  
  336.   if (status < 0) {
  337.     fprintf(stderr, "sprintf error %d\n", status);
  338.     abort_load(command, NULL, NULL);
  339.     return(-1);
  340.   }
  341. #endif
  342.  
  343.   if (strlen(command) >= command_length) {
  344.     fprintf(stderr, "load command overflowed buffer by %d chars\n",
  345.         strlen(command) - command_length);
  346.     abort_load(command, NULL, NULL);
  347.     return(-1);
  348.   }
  349.  
  350.   /* run the ld comand */
  351.   if (system(command) != 0 ) {
  352.     fprintf(stderr, "ld command failed\n");
  353.     abort_load(command, NULL, output_file);
  354.     return(-1);
  355.   }
  356.  
  357.   free(command);
  358.  
  359.   /* open the output file */
  360.   output_file_desc = fopen(output_file, "r");
  361.   if (output_file_desc == NULL ) {
  362.     fprintf(stderr, "unable to open output file\n");
  363.     abort_load(NULL, NULL, output_file);
  364.     return(-1);
  365.   }
  366.  
  367.   /* use our own buffer to prevent calls to malloc etc. */
  368.   setbuf(output_file_desc, output_file_buffer);
  369.  
  370.   /* read the output file header */
  371.   status = fread((char *)&output_file_header, sizeof(struct exec), 1,
  372.          output_file_desc);
  373.   if (status != 1) {
  374.     fprintf(stderr, "couldn't read output file header\n");
  375.     abort_load(NULL, output_file_desc, output_file);
  376.     return(-1);
  377.   }
  378.  
  379.   if (BAD_MAGIC(output_file_header)) {
  380.     fprintf(stderr, "output file has bad magic number %d\n",
  381.         output_file_header.a_magic);
  382.     abort_load(NULL, output_file_desc, output_file);
  383.     return(-1);
  384.   }
  385.   loaded_size = output_file_header.a_text + output_file_header.a_data;
  386.   required_size = loaded_size + output_file_header.a_bss;
  387.  
  388.   /* get required memory */
  389.   actual_load_point = (long) sbrk(required_size);
  390.   if (actual_load_point < 0L) {
  391.     fprintf(stderr, "sbrk(%ld) failed\n", required_size);
  392.     abort_load(NULL, output_file_desc, output_file);
  393.     return(-1);
  394.   }
  395.  
  396.   /* make sure nothing went wrong */
  397.   if (actual_load_point != load_point) {
  398.     fprintf(stderr, "storage pointer changed before file could be loaded\n");
  399.     abort_load(NULL, output_file_desc, output_file);
  400.     return(-1);
  401.   }
  402.   
  403.   /* go to beginning of text */
  404.   if (fseek(output_file_desc, TEXT_OFFSET(output_file_header), 0)
  405.       < 0) {
  406.     fprintf(stderr, "unable to seek to beginning of linked file text\n");
  407.     abort_load(NULL, output_file_desc, output_file);
  408.     return(-1);
  409.   }
  410.  
  411.   /* read the text and data segments */
  412.   if (fread((char *)load_point, 1, loaded_size, output_file_desc)
  413.       !=
  414.       loaded_size) {
  415.     fprintf(stderr, "unable to load linked file\n");
  416.     abort_load(NULL, output_file_desc, output_file);
  417.     return(-1);
  418.   }
  419.  
  420.   if (0 != fclose(output_file_desc))
  421.     fprintf(stderr, "unable to close file %s\n", output_file);
  422.  
  423.   return(0);  /* We made it! */
  424. }
  425.  
  426. /* Print a message and then clean up. */
  427.  
  428. void
  429. abort_load( char *command, FILE *file_desc, char *filename )
  430. {
  431.   fprintf(stderr, "aborting dynamic load\n");
  432.   if (command != NULL)
  433.     free(command);
  434.   if (file_desc != NULL)
  435.     if (0 != fclose(file_desc))
  436.       fprintf(stderr, "unable to close file %s\n", filename);
  437.   if (filename != NULL)
  438.     if (0 != unlink(filename))
  439.       fprintf(stderr, "unable to delete file %s\n", filename);
  440. }
  441.  
  442.  
  443. #else /* !ANCIENT_DYNLOAD */
  444.  
  445. /*-------------------------------------------------------------------
  446.    Dummy definition in case we don't have a clue.
  447.  */
  448.  
  449. int
  450. dynamic_load( char *user_args )
  451. {
  452.   fprintf(stderr, "Dynamic .o loading not implemented\n");
  453.   return -1;
  454. }
  455.  
  456. #endif
  457.  
  458.  
  459. /* Stub for dynamic_load() that can be called from Scheme using
  460.    external call interface */
  461.  
  462. scheme_value
  463. s48_dynamic_load( long nargs, scheme_value *argv )
  464. {
  465.   scheme_value arg;
  466.  
  467.   if (nargs != 1) return(SCHFALSE);
  468.  
  469.   arg = argv[0];
  470.   
  471.   if (!STRINGP(arg)) return(SCHFALSE);
  472.  
  473.   if (0 == dynamic_load(&STRING_REF(arg, 0)))
  474.     return(SCHTRUE);
  475.   else
  476.     return(SCHFALSE);
  477. }
  478.